     package inet.sms.process;
     
     import inet.db.common.DBException;
import inet.db.ems.MTQueueDAO;
import inet.pdu.SMSResponse;
import inet.sms.network.NetworkConfig;
import inet.sms.network.SessionManager;
import inet.util.Logger;
import inet.util.Queue;

import java.math.BigDecimal;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import red.sms.db.dao.SmsMtDAO;
import red.sms.db.entity.SmsMT;
import red.sms.util.IConstant;
import sms.pdu.Submit;
     
     public class ResponseProcessor extends Thread
     {
     private Logger logger = null;
     
     private Queue responseQueue = null;
     private MTQueueDAO mtQueueDAO = null;
     
       public ResponseProcessor() {
       this.responseQueue = Server.getResponseQueue();
     
       this.mtQueueDAO = new MTQueueDAO();
       this.logger = new Logger(super.getClass().getName());
       }
     
       public void processResponse(SMSResponse response)
         throws DBException
       {
       String receiverNumber = response.getReceiverNumber();
    	   //String receiverNumber = "841685458982";
       /*if ((receiverNumber == null) || ((response.getMessageType() != 1) && (response.getMessageType() != 11)))
         receiverNumber = response.getSenderNumber();*/
       if (NetworkConfig.isNetworkEnabled())
         {
    	   try{
    		   Submit submit = new Submit(response.getCommandCode(), response.getServiceNumber(), response.getSenderNumber(), response.getReceiverNumber(), response.getMessageType(), response.getContentType(), response.getRequestId().toString(), response.getInfo(), 0, response.getDestinationPort(), response.getCdr_key_request(), response.getTotalMessage());
    	         SessionManager.getInstance().getDataSendQueue().enqueue(submit);	   
    	   }catch(Exception e){
    		   e.printStackTrace();
    	   }
         
         }
         else {
         this.mtQueueDAO.insertRow(response.getSenderNumber(), receiverNumber, response.getServiceNumber(), response.getMobileOperator(), response.getCommandCode(), response.getContentType(), response.getMessageType(), response.getInfo(), response.getRawInfo(), response.getRequestId());
         }
       }  
       
       
       public void run(){
    	   while (Server.isRunning())
    		   try{
    			   //STEP 1: Get SMS message from MT table to send SMS message
    			   if(SmsMtDAO.hasMessage()){
    				   SmsMT smsMt = SmsMtDAO.dequeue();
    				   SMSResponse smsResponse = new SMSResponse();
    				   smsResponse.setRequestId(new BigDecimal(smsMt.getRequestId()));
    				   smsResponse.setReceiverNumber(smsMt.getPhoneNumber());
    				   smsResponse.setInfo(smsMt.getInfo());
    				   smsResponse.setServiceNumber(smsMt.getMaDichVu());
    				   smsResponse.setContentType(IConstant.SMS_RESPONSE.DEFAULT);
    				   smsResponse.setMessageType(IConstant.SMS_RESPONSE.DEFAULT);
    				   smsResponse.setSenderNumber(smsMt.getMaDichVu());
    				   smsResponse.setReceiverNumber(smsMt.getPhoneNumber());
        			//STEP 2: LOG TO FILE
        			   String content="From: "+smsResponse.getServiceNumber()+"      To: "+smsResponse.getReceiverNumber()+"      info: "+smsResponse.getInfo()+"     on: "+ getTimeNow();
        	    	   String fileLog="receiving_";
        	    	   this.logger.log(content, fileLog);
        			//STEP 3: Sending SMS message response
        	    	   processResponse(smsResponse);   
    			   }
    		   }catch(Exception e){
    			   this.logger.error("error1: " + e.getMessage() +  "on :" + getTimeNow() + "\n");
    		   }
       }
       
       /*public void run()
       {
       while (Server.isRunning())
           try
           {
           SMSResponse smsResponse = (SMSResponse)this.responseQueue.dequeue();
           String command=smsResponse.getCommandCode();
           if (command.equals("HT")){
        	  smsResponse.setInfo("Ban da gui OTP thanh cong");  
           }else{
        	  smsResponse.setInfo("Tin nhan khong hop le");    
           }
          
          
          //Ghi log ra File
            String content="From: "+smsResponse.getServiceNumber()+"      To: "+smsResponse.getReceiverNumber()+"      info: "+smsResponse.getInfo()+"     on: "+ getTimeNow();
    		String fileLog="receiving_";
    		this.logger.log(content, fileLog);
    	  //Ghi log DB
    		//DBProcess db=new DBProcess();
    		//int requestId=db.saveMO(dauSo,sdt,smsInfo);
    		BigDecimal requestId=smsResponse.getRequestId();
    		int destPort=0;
    		String telco=smsResponse.getServiceNumber();
    		String sdt=smsResponse.getReceiverNumber();
    		String smsInfo=smsResponse.getInfo();
    		int request=requestId.intValue();
    		//no saving here 11.09.2013
    		//db.saveMT(request, destPort, telco, sdt, smsInfo);
            processResponse(smsResponse);       
           }
           catch (DBException ex) {
           this.logger.error("run_1: " + ex.getMessage());
           //Server.rebuildSmppDBPool();
           } catch (Exception ex) {
           this.logger.error("run_2: " + ex.getMessage());
           }
       }*/
      
       public static String getTimeNow(){
     	  String s;
     	  Format formatter;
     	  Date date = new Date();

     	  formatter = new SimpleDateFormat("hh:mm:ss");
     	  s = formatter.format(date);
     	  return s;
     	  }
       }
     


 